package com.ruoyi.common.filter;


import org.slf4j.MDC;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

public class RequestHeaderFilter implements Filter {
    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest httpRequest = (HttpServletRequest) request;
        String headerValue = httpRequest.getHeader("Header-Name"); // 替换为具体的Header名称
        MDC.put("header-name", headerValue); // 使用小写字母和短横线命名MDC变量，避免与系统属性冲突
        try {
            chain.doFilter(request, response);
        } finally {
            MDC.remove("header-name"); // 清理MDC数据，避免内存泄漏或错误数据保留
        }
    }
}